package com.example.lenovo.sbns.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.example.lenovo.sbns.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import com.baidu.mapapi.BMapManager;


public class classifyPage extends Fragment {


    private ArrayAdapter<String> sugAdapter = null;
    private int load_index = 0;
    private double latitude,longitude;
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LocationClient locationClient;
    boolean isFirstLoc = true;// 是否首次定位
    private RadioButton delicious, hospital, shop;
    private RadioGroup group;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        SDKInitializer.initialize(getActivity().getApplicationContext());
        View view = inflater.inflate(R.layout.fragment_classify_page, container, false);

        group = (RadioGroup) view.findViewById(R.id.GroupButton);
        hospital = (RadioButton) view.findViewById(R.id.Hospital);
        delicious = (RadioButton) view.findViewById(R.id.Delicious);
        shop = (RadioButton) view.findViewById(R.id.Shop);

        mMapView = (MapView) view.findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

        // 构造定位数据
        locationClient = new LocationClient(getActivity().getApplicationContext()); // 实例化LocationClient类
        locationClient.registerLocationListener(myListener); // 注册监听函数
        setLocationOption();   //设置定位参数
        locationClient.start(); // 开始定位
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); // 设置为一般地图


    return view;

    }

    /**
     * 定义定位监听器
     */
    public BDLocationListener myListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {

            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null)
                return;

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            /**
             * 获取当前位置经纬度
             */
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            System.out.println("————监听器经度："+latitude);
            System.out.println("————监听器纬度："+longitude);
            mBaiduMap.setMyLocationData(locData);    //设置定位数据

            /*附近查询*/
            final PoiSearch mPoiSearch = PoiSearch.newInstance();
            System.out.println("A01");
            OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){
                public void onGetPoiResult(PoiResult result){
                    //获取POI检索结果
                    for(int i = 0;i<result.getAllPoi().size();i++){
                        System.out.println("@@@@@@" + result.getAllPoi().get(i).name);
                        /**
                         * 在地图上标记查询结果
                         */
                        mBaiduMap.clear();
                        PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
                        poiOverlay.setData(result);
                        poiOverlay.addToMap();
                        poiOverlay.zoomToSpan();

                    }
                }
                public void onGetPoiDetailResult(PoiDetailResult result){
                    //获取Place详情页检索结果
                }
            };

            group.setOnCheckedChangeListener(
                    new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId){
                                case R.id.Hospital:
                                    /*检索医院*/
                                    System.out.println("A01");
                                    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){
                                        public void onGetPoiResult(PoiResult result){
                                            //获取POI检索结果
                                            for(int i = 0;i<result.getAllPoi().size();i++){
                                                System.out.println("@@@@@@" + result.getAllPoi().get(i).name);
                                                /**
                                                 * 在地图上标记查询结果
                                                 */
                                                mBaiduMap.clear();
                                                PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
                                                poiOverlay.setData(result);
                                                poiOverlay.addToMap();
                                                poiOverlay.zoomToSpan();

                                            }
                                        }
                                        public void onGetPoiDetailResult(PoiDetailResult result){
                                            //获取Place详情页检索结果
                                        }
                                    };
                                    System.out.println("A02");
                                    mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
                                    System.out.println("A03");
                                    PoiNearbySearchOption poiNearbySearchOption = new PoiNearbySearchOption();
                                    LatLng latLng = new LatLng(latitude,longitude);
                                    System.out.println("A04");
                                    poiNearbySearchOption.location(latLng).radius(10000).keyword("医院");
                                    System.out.println("A05");
                                    mPoiSearch.searchNearby(poiNearbySearchOption);
                                    System.out.println("A06");
                                    break;

                                case R.id.Delicious:

                                    /*检索超市*/
                                    System.out.println("A01");
                                    OnGetPoiSearchResultListener poiListenerone = new OnGetPoiSearchResultListener(){
                                        public void onGetPoiResult(PoiResult result){
                                            //获取POI检索结果
                                            for(int i = 0;i<result.getAllPoi().size();i++){
                                                System.out.println("@@@@@@" + result.getAllPoi().get(i).name);
                                                /**
                                                 * 在地图上标记查询结果
                                                 */
                                                mBaiduMap.clear();
                                                PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
                                                poiOverlay.setData(result);
                                                poiOverlay.addToMap();
                                                poiOverlay.zoomToSpan();

                                            }
                                        }
                                        public void onGetPoiDetailResult(PoiDetailResult result){
                                            //获取Place详情页检索结果
                                        }
                                    };
                                    System.out.println("A02");
                                    mPoiSearch.setOnGetPoiSearchResultListener(poiListenerone);
                                    System.out.println("A03");
                                    PoiNearbySearchOption poiNearbySearchOptionone = new PoiNearbySearchOption();
                                    LatLng latLngone = new LatLng(latitude,longitude);
                                    System.out.println("A04");
                                    poiNearbySearchOptionone.location(latLngone).radius(10000).keyword("饭店");
                                    System.out.println("A05");
                                    mPoiSearch.searchNearby(poiNearbySearchOptionone);
                                    System.out.println("A06");
                                    break;

                                case R.id.Shop:

                                    /*检索饭店*/
                                    System.out.println("A01");
                                    OnGetPoiSearchResultListener poiListenertwo = new OnGetPoiSearchResultListener(){
                                        public void onGetPoiResult(PoiResult result){
                                            //获取POI检索结果
                                            for(int i = 0;i<result.getAllPoi().size();i++){
                                                System.out.println("@@@@@@" + result.getAllPoi().get(i).name);
                                                /**
                                                 * 在地图上标记查询结果
                                                 */
                                                mBaiduMap.clear();
                                                PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);
                                                poiOverlay.setData(result);
                                                poiOverlay.addToMap();
                                                poiOverlay.zoomToSpan();

                                            }
                                        }
                                        public void onGetPoiDetailResult(PoiDetailResult result){
                                            //获取Place详情页检索结果
                                        }
                                    };
                                    System.out.println("A02");
                                    mPoiSearch.setOnGetPoiSearchResultListener(poiListenertwo);
                                    System.out.println("A03");
                                    PoiNearbySearchOption poiNearbySearchOptiontwo = new PoiNearbySearchOption();
                                    LatLng latLngtwo = new LatLng(latitude,longitude);
                                    System.out.println("A04");
                                    poiNearbySearchOptiontwo.location(latLngtwo).radius(10000).keyword("超市");
                                    System.out.println("A05");
                                    mPoiSearch.searchNearby(poiNearbySearchOptiontwo);
                                    System.out.println("A06");
                                    break;
                            }
                        }
                    }
            );


            if (isFirstLoc) {
                isFirstLoc = false;

                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 16);   //设置地图中心点以及缩放级别
                //              MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(u);
            }
        }
    };

    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
    private void setLocationOption() {

        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开GPS
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式
        option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true); // 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向

        locationClient.setLocOption(option);
    }




}